{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "88ed495f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "2.0\n",
      "7.0\n"
     ]
    }
   ],
   "source": [
    "#chp3_变量和基本运算#\n",
    "\n",
    "#运算关系符号#\n",
    "\n",
    "a=8%5\n",
    "b=9.6//4\n",
    "c=28.9364//4\n",
    "print(a)#mod 取余数\n",
    "print(b)#floor divsion 取离结果最近的整数\n",
    "print(c)#整数结果只取了一位小数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "db1fb246",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "12\n",
      "0b1100\n"
     ]
    }
   ],
   "source": [
    "#关系运算符 输出的值是true or false#\n",
    "#位运算符（注意这里的bin函数是输出二进制的形式，前面有0b或者-0b）#\n",
    "\n",
    "a=28#11100\n",
    "b=15#1111\n",
    "print(a&b)#用他们的二进制来做与运算\n",
    "print(bin(a&b))#输出二进制结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "12dafc41",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0b11111\n"
     ]
    }
   ],
   "source": [
    "print(bin(a|b))#或运算也是对应二进制#"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "22efa003",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "0\n",
      "False\n",
      "False\n",
      "True\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "#表示与或也可以用and和or，ab为逻辑变量时二者相同，为数字时有区别#\n",
    "#and中有0则返回0，无0返回后一个值；or中全0返回0，否则返回第一个非0值#\n",
    "print(2 and 0)\n",
    "print(2 and 1)\n",
    "print(2 or 1)\n",
    "print(0 or 0)\n",
    "#逻辑变量（true false）的与或#\n",
    "print((3>4)and(5!=4))\n",
    "print((3>4)&(5!=4))\n",
    "print((3>4)or(5!=4))\n",
    "print((3>4)|(5!=4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "366129e3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0b10011\n"
     ]
    }
   ],
   "source": [
    "# ^ 表示异或，而**表示乘方，注意区分#\n",
    "a=28#11100\n",
    "b=15#1111\n",
    "print(bin(a^b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "98ab4b7f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0b111\n",
      "0b1111000\n"
     ]
    }
   ],
   "source": [
    "# >> << 表示右移和左移,这种方法只适用于二进制#\n",
    "print(bin(a>>2))#7\n",
    "print(bin(b<<3))#120"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5e19cfd7",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
